package com.example.uit_management;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class Activity_list_testscheduleID extends Activity {

	private JsonParser_scheduleID Json_testschedule=new JsonParser_scheduleID();
	private ListView list;	
	private ArrayAdapterTestScheduleID adapter;	
	private ArrayList<HashMap<String, String>> array_json_testscheduleID=new ArrayList<HashMap<String, String>>();
	private HashMap<String, String> json_testscheduleID=new HashMap<String, String>();
	//private ArrayList<HashMap<String, String>> array_MAP=new ArrayList<HashMap<String, String>>();
	////khai bao bien cho spinner
	/** spinner */
	private Spinner spinner;	
	/** Mang nien khoa hoc thi */
	ArrayList<String> ArrayList ;
	private ArrayAdapter spinnerArrayAdapter;
	/** Nien khoa **/
	private static String Spinner_semester="Tất cả";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_testschedule_id);
        Menu_GridView.dialog.dismiss();
        Log.e(Activity_list_testscheduleID.class.toString(), "Vao schedule ");
        // Thong bao ngay trong tuan
        try{
        	array_json_testscheduleID = Json_testschedule.getMap();
        	list=(ListView)findViewById(R.id.list_testscheduleID);
        // call constructor method of adapter   
        // xap xep theo nam hoc
        Collections.sort(array_json_testscheduleID, new ArrayMap_sore_semester());        
        //////////////////////////////////
        // tao spinner chon hoc ki hoc
        ArrayList = new ArrayList<String>();
        ArrayList.clear();      
        HashMap<String, String> json_current;
        HashMap<String, String> json_before;
        for (int i = 0; i < array_json_testscheduleID.size(); i++) {
        	if(i == 0){
        		json_current = array_json_testscheduleID.get(i);
        		ArrayList.add("Học kì:"+json_current.get(JsonParser_schedule.KEY_semesterIndex)+"_Năm :"+json_current.get(JsonParser_schedule.KEY_semesterYear));
        	}else{    
	        	json_current =array_json_testscheduleID.get(i);
	        	json_before =array_json_testscheduleID.get(i-1);
	        	if(!((json_current.get(JsonParser_scheduleID.KEY_semesterYear)+json_current.get(JsonParser_scheduleID.KEY_semesterIndex)).equals(json_before.get(JsonParser_scheduleID.KEY_semesterYear)+json_before.get(JsonParser_scheduleID.KEY_semesterIndex)))){
	        		ArrayList.add("Học kì:"+json_current.get(JsonParser_scheduleID.KEY_semesterIndex)+"_Năm :"+json_current.get(JsonParser_scheduleID.KEY_semesterYear));
        		}
        	}			
		}
		/////////////////////////////
		// Spriner
		///////////////////////////
		// set semetester for spinner
		Spinner_semester="Tất cả";
		spinner = (Spinner) findViewById(R.id.spinner_testscheduleID);
		spinnerArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,ArrayList);
		spinner.setAdapter(spinnerArrayAdapter);		
		spinner.setOnItemSelectedListener(Spinner_ItemSelectedListener_shedule);
		spinner.setSelection(ArrayList.size()-1);
		// set lang nghe sellectItem
        adapter =new ArrayAdapterTestScheduleID(this, array_json_testscheduleID);
        // add adapter into list
        if(array_json_testscheduleID.size()==0){
        	Toast.makeText(this, "Không có môn nào", Toast.LENGTH_LONG).show();
        }
        list.setAdapter(adapter);     
        }catch(Exception e){
        	e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity__score, menu);
        return true;
    }  
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_back:{
        	finish();  
        	Intent intent = new Intent(getApplicationContext(),Menu_GridView.class);
				startActivity(intent);				
				break;
        	}
        }
        return true;
    }   
    static public String getSemester_Spinner(){
    	return Spinner_semester;
    }
    private OnItemSelectedListener Spinner_ItemSelectedListener_shedule = new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            // your code here
        	// khi thay doi staue        	          
            Spinner_semester=spinner.getSelectedItem().toString();
        	Log.e(Activity_list_testscheduleID.class.toString(),"SPinner chose select :" +spinner.getSelectedItem().toString());
        	// set lai custom list   
        	if(Activity_list_testscheduleID.getSemester_Spinner().toString().equals("Tất cả")){
        		SetAdaptet();
        	}else{
        		SetAdaptet_Filter();
        	}
        	
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }
    };
    private void SetAdaptet(){
    	// Get Id of list
    	list=(ListView)findViewById(R.id.list_testscheduleID);  
    	adapter =new ArrayAdapterTestScheduleID(this, array_json_testscheduleID);
    	list.setAdapter(adapter);
    	
    }
    private void SetAdaptet_Filter(){
    	// Get Id of list
    	ArrayList<HashMap<String, String>> array_Map_filter=new ArrayList<HashMap<String, String>>();
    	HashMap<String, String> Map;
    	for (int i = 0; i < array_json_testscheduleID.size(); i++) {
    		Map=array_json_testscheduleID.get(i);
    		if(getSemester_Spinner().split("_")[1].toString().split("\\:")[1].equals(Map.get(JsonParser_scheduleID.KEY_semesterYear))&&(getSemester_Spinner().split("_")[0].toString().split("\\:")[1].equals(Map.get(JsonParser_scheduleID.KEY_semesterIndex)))){
    			array_Map_filter.add(Map);
        	}
		}    
    	list=(ListView)findViewById(R.id.list_testscheduleID);
    	adapter =new ArrayAdapterTestScheduleID(this, array_Map_filter);
    	list.setAdapter(adapter);
    }
    
}

